﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Data
{
    public class CustomerData
    {
        Connect cn = new Connect();
        public CustomersEntity GetCustomer(CustomersEntity ctE)
        {
            int Npara = 1;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@CustID";
            value[0] = ctE.CustID;
            string sql = "dbo.CustomersSelectByID";
            DataTable dt = new DataTable();
            dt = cn.GetData(sql, name, value, Npara);
            CustomersEntity ctmE = new CustomersEntity
            {
                CustID = (int)dt.Rows[0]["CustID"],
                Name = dt.Rows[0]["Name"].ToString(),
                Phone =dt.Rows[0]["Phone"].ToString(),
                Email =dt.Rows[0]["Email"].ToString(),
                CustAddress =dt.Rows[0]["CustAddress"].ToString()
            };
            return ctmE;
        }
        public DataTable ViewHistory(CardsEntity cE)
        {
            LogData lTD = new LogData();
            List<LogsEntity> listLog = lTD.ReadLog(cE);
            DataTable dt = new DataTable();
            dt.Columns.Add("ATM (Location)");
            dt.Columns.Add("Type");
            dt.Columns.Add("Date");
            dt.Columns.Add("Amount");
            dt.Columns.Add("Details");
            for (int i = 0; i <= listLog.Count;i++ )
            {
                DataRow row = dt.NewRow();
                row[0] = listLog[i].ATM.Branch;
                row[1] = listLog[i].LogType.Description;
                row[2] = listLog[i].LogDate;
                row[3] = listLog[i].Amount;
                row[4] = listLog[i].Details;
                dt.Rows.Add(row);
            }
            return dt;
        }

        public void Withdraw(Decimal amount, int accountID, string cardNo)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("dbo.Withdraw", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@Amount", SqlDbType.Decimal).Value = amount;
                    cmd.Parameters.Add("@AccountIDWithdraw", SqlDbType.Int).Value = accountID;
                    cmd.Parameters.Add("@ATMID", SqlDbType.Int).Value = 1;
                    cmd.Parameters.Add("@CardNo", SqlDbType.NVarChar).Value = cardNo;
                    cmd.Parameters.Add("@LogDate", SqlDbType.DateTime).Value = DateTime.Now;
                    cmd.Parameters.Add("@Details", SqlDbType.NVarChar).Value = "suc";
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public Decimal GetBalance(int AccountID)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("AccountsSelectByBalance", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@AccountID", SqlDbType.Int).Value = AccountID;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    DataTable dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    string x = dt.Rows[0][0].ToString();
                    Decimal balance = Decimal.Parse(x);
                    return balance;
                }
            }
        }

        public Decimal GetMoney(int MoneyID)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("MoneysSelectByMoneyValue", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@MoneyID", SqlDbType.Int).Value = MoneyID;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    DataTable dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    string x = dt.Rows[0][0].ToString();
                    Decimal balance = Decimal.Parse(x);
                    return balance;
                }
            }
        }
    }
    

}
